/** @file         app.c
 *  @brief        简要说明
 *  @details      详细说明
 *  @author       lzm
 *  @date         2023-04-21 23:17:03
 *  @version      v1.0
 *  @copyright    Copyright By lizhuming, All Rights Reserved
 *
 **********************************************************
 *  @LOG 修改日志:
 **********************************************************
*/

#include <stdio.h>
#include "tx_api.h"

#define DEMO_STACK_SIZE         1024
#define DEMO_BYTE_POOL_SIZE     (8*1024*1024)


/* Define the ThreadX object control blocks...  */
TX_THREAD               thread_a;
TX_THREAD               thread_b;
TX_BYTE_POOL            byte_pool;
UCHAR                   memory_area[DEMO_BYTE_POOL_SIZE];

/* Define thread prototypes.  */
void    thread_a_entry(ULONG thread_input);
void    thread_b_entry(ULONG thread_input);


/* Define main entry point.  */
int main()
{
    printf("Enter\r\n");
    /* Enter the ThreadX kernel.  */
    tx_kernel_enter();
}


/* Define what the initial system looks like.  */
void tx_application_define(void *first_unused_memory)
{

    CHAR    *pointer = TX_NULL;


    /* Create a byte memory pool from which to allocate the thread stacks.  */
    tx_byte_pool_create(&byte_pool, "byte pool", memory_area, DEMO_BYTE_POOL_SIZE);

    /* Put system definition stuff in here, e.g. thread creates and other assorted
       create information.  */

    /* Allocate the stack for thread 0.  */
    tx_byte_allocate(&byte_pool, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);

    /* Create the main thread.  */
    tx_thread_create(&thread_a, "thread a", thread_a_entry, 0,  
            pointer, DEMO_STACK_SIZE, 
            1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);

    tx_byte_allocate(&byte_pool, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);

    tx_thread_create(&thread_b, "thread b", thread_b_entry, 0,  
            pointer, DEMO_STACK_SIZE, 
            1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
}

/* Define the test threads.  */
void thread_a_entry(ULONG thread_input)
{
    ULONG   thread_counter = 0;


    /* This thread simply sits in while-forever-sleep loop.  */
    while(1)
    {

        printf("aaa:%d\r\n", thread_counter);
        /* Increment the thread counter.  */
        thread_counter++;

        /* Sleep for 100 ticks.  */
        tx_thread_sleep(100);
    }
}

void thread_b_entry(ULONG thread_input)
{
    ULONG   thread_counter = 0;

    /* This thread simply sits in while-forever-sleep loop.  */
    while(1)
    {

        printf("bbb:%d\r\n", thread_counter);
        /* Increment the thread counter.  */
        thread_counter++;

        /* Sleep for 100 ticks.  */
        tx_thread_sleep(50);
    }
}
